package com.cloudservice.ccs.biz.impl;

import com.cloudservice.ccs.biz.ISessionBiz;
import com.cloudservice.ccs.entity.Session;
import com.cloudservice.ccs.exception.SessionException;
import com.cloudservice.ccs.mapper.SessionMapper;
import com.cloudservice.ccs.model.SessionWithLatestMessage;
import com.cloudservice.ccs.websocket.SessionServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SessionImpl implements ISessionBiz {

    private Logger logger = LoggerFactory.getLogger(SessionImpl.class);

    @Autowired
    private SessionMapper sessionMapper;

    @Override
    public void save(Session session) {
        int result = sessionMapper.save(session);
        if (result == 0) {
            throw new SessionException("Create Session entity error");
        }
        Integer customerServiceId = session.getCustomerServiceId();
        if (customerServiceId != null) {
            try {
                SessionServer.sendSessionList(customerServiceId, sessionMapper);
            } catch (IOException e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Error when notifying new session list: " + e.getClass().getName() + ": " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void update(Integer id, Session session) {
        int result = sessionMapper.update(id, session);
        if (result == 0) {
            throw new SessionException("Update Session entity error");
        }
    }

    @Override
    public void partialUpdate(Integer id, Map<String, Object> fields) {
        int result = sessionMapper.partialUpdate(id, fields);
        if (result == 0) {
            throw new SessionException("Partial update Session failed");
        }
    }

    @Override
    public void delete(Integer id) {
        Session session = sessionMapper.getOne(id);
        if (session == null) {
            throw new SessionException("No such Session entity");
        }
        Integer customerServiceId = session.getCustomerServiceId();
        int result = sessionMapper.delete(id);
        if (result == 0) {
            throw new SessionException("Delete Session entity error");
        }
        if (id != null) {
            try {
                SessionServer.sendSessionList(customerServiceId, sessionMapper);
            } catch (IOException e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Error when notifying new session list: " + e.getClass().getName() + ": " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public Session getOne(Integer id) {
        Session session = sessionMapper.getOne(id);
        if (session == null) {
            throw new SessionException("Get Session entity error: no such entity");
        }
        return session;
    }

    @Override
    public List<Session> getAll() {
        return sessionMapper.getAll();
    }

    @Override
    public List<SessionWithLatestMessage> getAllByCustomerServiceId(Integer customerServiceId) {
        return sessionMapper.getWithLatestMessage(customerServiceId);
    }

    @Override
    public List<SessionWithLatestMessage> getActiveByCustomerServiceId(Integer customerServiceId) {
        return sessionMapper.getWithActiveStatusWithLatestMessage(customerServiceId, true);
    }

    @Override
    public List<SessionWithLatestMessage> getHistoryByCustomerServiceId(Integer customerServiceId) {
        return sessionMapper.getWithActiveStatusWithLatestMessage(customerServiceId, false);
    }

    @Override
    public Session getByCustomerServiceAndVisitor(Integer customerServiceId, String visitorIp) {
        Session session = sessionMapper.getByCustomerServiceAndVisitor(customerServiceId, visitorIp);
        if (session == null) {
            throw new SessionException("指定的会话不存在");
        }
        return session;
    }

    @Override
    public Session getAvailableSession(String visitorIp) {
        return sessionMapper.getAvailableSession(visitorIp);
    }
}
